package com.xinghou.platform.dao.wzk.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.wzk.ShowcardimgDAO;
import com.xinghou.platform.model.wzk.Showcardimg;

@Repository
public class ShowcardimgDAOImpl implements ShowcardimgDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public Showcardimg getShowcardimgById(String id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT imgid,infoid,imgpath FROM wzk.showcardimg where imgid = ?";
		Showcardimg showcardimg = jdbcTemplate.queryForObject(sql, new Object[] { id }, new ShowcardimgMapper());
		CustomerContextHolder.clearCustomerType();
		return showcardimg;
	}

	@Override
	public void updateShowcardimg(Showcardimg showcardimg)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("UPDATE wzk.showcardimg SET infoid = ?, imgpath = ?")
		.append(" where imgid=?");
		
       Object[] params = {showcardimg.getInfoid(),showcardimg.getImgpath(),showcardimg.getImgid()
     };

    CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
    int ret = jdbcTemplate.update(strSQL.toString(),params);
    CustomerContextHolder.clearCustomerType();
		
	}
	
	public List<Showcardimg> getShowcardimgById(int infoid) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT imgid,infoid,imgpath FROM wzk.showcardimg where infoid = ?";
		List<Showcardimg> showcardimgs = jdbcTemplate.query(sql,new Object[] {infoid }, new ShowcardimgMapper());
		CustomerContextHolder.clearCustomerType();
		return showcardimgs;
	}
	
	private static final class ShowcardimgMapper implements RowMapper<Showcardimg> {
		public Showcardimg mapRow(ResultSet rs, int rowNum) throws SQLException {
			Showcardimg showcardimg = new Showcardimg();
			showcardimg.setImgid(rs.getInt("imgid"));
			showcardimg.setImgpath(rs.getString("imgpath"));
			showcardimg.setInfoid(rs.getInt("infoid"));
			return showcardimg;
		}
	}
	
}
